package com.irdstudio.efp.console.service.impl;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SEQ;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.console.service.dao.SRoleDao;
import com.irdstudio.efp.console.service.domain.SRole;
import com.irdstudio.efp.console.service.facade.SRoleService;
import com.irdstudio.efp.console.service.vo.SRoleVO;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 系统角色表				<p>
 * @author chenqm1
 * @date 2018-05-03
 */
@Service("sRoleService")
public class SRoleServiceImpl extends AbstractFrameworkService implements SRoleService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(SRoleServiceImpl.class);
	public static final PatternDescriptor PATTERN = DefaultPatternDescriptor.of("R" + SEQ, 4);
	@Autowired
	private SRoleDao sRoleDao;
	
	@Autowired
	@Qualifier("defaultSeqService")
	private PatternedLimitableSeqService defaultSeqService;
	/**
	 * 新增操作
	 */
	@Override
	public int insertSRole(SRoleVO inSRoleVo) {
		logger.debug("当前新增数据为:"+ inSRoleVo.toString());
		int num = 0;
		try {
			String roleCode = defaultSeqService.getPatternedSequence("ROLE_CODE", PATTERN);
			inSRoleVo.setRoleCode(roleCode);
			SRole sRole = new SRole();
			beanCopy(inSRoleVo, sRole);
			num = sRoleDao.insertSRole(sRole);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(SRoleVO inSRoleVo) {
		logger.debug("当前删除数据条件为:" + inSRoleVo);
		int num = 0;
		try {
			SRole sRole = new SRole();
			beanCopy(inSRoleVo, sRole);
			/**
			 * 【2019D0509】【(问题编号)】
			 * @date 2019/11/20
			 * num = sRoleDao.queryRoleBindUserTotal(sRole);
			 * 			if ( num > 0) {
			 * 				num = -2;
			 *                        }else {
			 */
			num = sRoleDao.deleteByPk(sRole);
			if (num > 0) {
				num += sRoleDao.deleteRoleRescRule(sRole);
				num += sRoleDao.deleteRoleDataRule(sRole);
				num += sRoleDao.deleteRoleBindUser(sRole);
			}
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRoleVo + "删除的数据条数为" + num);
		return num;
	}


	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(SRoleVO inSRoleVo) {
		logger.debug("当前修改数据为:"+ inSRoleVo.toString());
		int num = 0;
		try {
			SRole sRole = new SRole();
			beanCopy(inSRoleVo, sRole);
			num = sRoleDao.updateByPk(sRole);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSRoleVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	@Override
	public SRoleVO queryByPk(SRoleVO inSRoleVo) {
		
		logger.debug("当前查询参数信息为:"+ inSRoleVo);
		try {
			SRole querySRole = new SRole();
			beanCopy(inSRoleVo, querySRole);
			SRole queryRslSRole = sRoleDao.queryByPk(querySRole);
			if (Objects.nonNull(queryRslSRole)) {
				SRoleVO outSRoleVo = beanCopy(queryRslSRole, new SRoleVO());
				logger.debug("当前查询结果为:"+ outSRoleVo.toString());
				return outSRoleVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleVO> queryAllOwner(SRoleVO sRoleVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		List<SRoleVO> list = null;
		try {
			List<SRole> sRoles = sRoleDao.queryAllOwnerByPage(sRole);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ sRoles.size());
			pageSet(sRoles, sRole);
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleVO> queryAllCurrOrg(SRoleVO sRoleVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		List<SRole> sRoles = sRoleDao.queryAllCurrOrgByPage(sRole);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+sRoles.size());
		List<SRoleVO> list = null;
		try {
			pageSet(sRoles, sRole);
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleVO> queryAllCurrDownOrg(SRoleVO sRoleVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		List<SRole> sRoles = sRoleDao.queryAllCurrDownOrgByPage(sRole);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ sRoles.size());
		List<SRoleVO> list = null;
		try {
			pageSet(sRoles, sRole);
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SRoleVO> queryRoleAll(SRoleVO sRoleVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		List<SRole> sRoles = sRoleDao.queryRoleAll(sRole);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ sRoles.size());
		List<SRoleVO> list = null;
		try {
			pageSet(sRoles, sRole);
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	@Override
	public int queryRoleBindUserTotal(SRoleVO inSRoleVo) {
		int total = 0;
		SRole sRole = new SRole();
		try {
			beanCopy(inSRoleVo, sRole);
			total = sRoleDao.queryRoleBindUserTotal(sRole);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return total;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SRoleVO> queryRoleByLcAndStByPage(SRoleVO sRoleVo) {
		
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		List<SRole> sRoles = sRoleDao.queryRoleByLcAndStByPage(sRole);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ sRoles.size());
		List<SRoleVO> list = null;
		try {
			pageSet(sRoles, sRole);
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SRoleVO> queryRoleAllByLoginLeage(SRoleVO inSRoleVo) {

		logger.debug("当前查询登录法人机构数据信息的参数信息为:" + inSRoleVo.getLoginUserLeageOrgCode());
		SRole sRole = beanCopy(inSRoleVo, new SRole());
		List<SRole> sRoles = sRoleDao.queryRoleAllByLoginLeage(sRole);
		logger.debug("当前查询登录法人机构数据信息的结果集数量为:"+ sRoles.size());
		List<SRoleVO> list = null;
		try {
			pageSet(sRoles, sRole);
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	/**
	 * 角色下用户列表数据查询
	 * 
	 * @return
	 */
	@Override
	public List<SRoleVO> queryRoleUserAll(SRoleVO inSRoleVo) {
		logger.debug("当前查询角色信息为:" + inSRoleVo);
		SRole sRole = beanCopy(inSRoleVo, new SRole());
		List<SRoleVO> list = null;
		try {
			List<SRole> sRoles = sRoleDao.queryRoleUserAllByPage(sRole);
			logger.debug("当前查询角色下用户信息的结果集数量为:" + sRoles.size());
			pageSet(sRoles, sRole);
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SRoleVO> queryAllOwnerNotPage(SRoleVO sRoleVo) {
		List<SRoleVO> list = null;
		logger.debug("不分页查询当前本人所属数据信息的参数为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		try {
			List<SRole> sRoles = sRoleDao.queryAllOwnerNotPage(sRole);
			logger.debug("不分页查询当前本人所属数据信息的结果集数量为:"+ sRoles.size());
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SRoleVO> queryAllCurrOrgNotPage(SRoleVO sRoleVo) {
		List<SRoleVO> list = null;
		logger.debug("不分页查询当前本人所属机构数据信息的参数信息为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		List<SRole> sRoles = sRoleDao.queryAllCurrOrgNotPage(sRole);
		logger.debug("不分页查询当前本人所属机构数据信息的结果集数量为:"+sRoles.size());
		try {
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SRoleVO> queryAllCurrDownOrgNotPage(SRoleVO sRoleVo) {
		List<SRoleVO> list = null;
		logger.debug("不分页查询当前本人所属机构及以下数据信息的参数信息为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		List<SRole> sRoles = sRoleDao.queryAllCurrDownOrgNotPage(sRole);
		logger.debug("不分页查询当前本人所属机构及以下数据信息的结果集数量为:"+ sRoles.size());
		try {
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<SRoleVO> queryAllByOrgs(SRoleVO sRoleVO) {
		List<SRoleVO> list = null;
		logger.debug("根据参数查询角色信息开始======>");
		SRole sRole = beanCopy(sRoleVO, new SRole());
		List<SRole> sRoles = sRoleDao.queryAllByOrgs(sRole);
		logger.debug("根据参数查询角色信息的结果集数量为:"+ sRoles.size());
		try {
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<SRoleVO> queryAllByCoditions(SRoleVO sRoleVO) {
		List<SRoleVO> list = null;
		logger.debug("根据参数查询角色信息开始======>");
		SRole sRole = beanCopy(sRoleVO, new SRole());
		List<SRole> sRoles = sRoleDao.queryAllByConditions(sRole);
		logger.debug("根据参数查询角色信息的结果集数量为:"+ sRoles.size());
		try {
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<SRoleVO> queryAllValidRole(SRoleVO sRoleVO) {
		List<SRoleVO> list = null;
		logger.debug("根据参数查询角色信息开始======>");
		SRole sRole = beanCopy(sRoleVO, new SRole());
		List<SRole> sRoles = sRoleDao.queryAllValidRole(sRole);
		logger.debug("根据参数查询角色信息的结果集数量为:"+ sRoles.size());
		try {
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<SRoleVO> queryRoleAllNotR0000(SRoleVO sRoleVo){
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRole sRole = beanCopy(sRoleVo, new SRole());
		List<SRole> sRoles = sRoleDao.queryRoleAllNotR0000(sRole);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ sRoles.size());
		List<SRoleVO> list = null;
		try {
			pageSet(sRoles, sRole);
			list = (List<SRoleVO>) beansCopy(sRoles, SRoleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}


}
